/**
 * @date 5:56:35 PM
 * SchoolAPIServiceImpl.java
 * Administrator
 * TODO
 */
package com.qs.education.service.impl;

import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import com.qs.education.bean.api.ResponseBean;
import com.qs.education.bean.api.ResponseListBean;
import com.qs.education.bean.beans.EducationSchoolBean;
import com.qs.education.bean.beans.EducationSchoolResponse;
import com.qs.education.bean.entity.EducationSchool;
import com.qs.education.bean.entity.EducationStudent;
import com.qs.education.bean.enums.ResponseStatus;
import com.qs.education.bean.service.BaseService;
import com.qs.education.service.ISchoolAPIService;
/**
 * @class SchoolAPIServiceImpl	
 * @author Warren
 * @description
 * @date Nov 7, 2018 
 */
@Service
public class SchoolAPIServiceImpl extends BaseService implements ISchoolAPIService{

	/**
	 * @author Warren
	 * @return List<EducationSchoolBean>
	 * @description 获取所有学校的列表
	 * @date Nov 7, 2018
	 */
	@Override
	public ResponseListBean<EducationSchoolBean> getList() {
		List<EducationSchool> list = iEducationSchoolDao.getAll(EducationSchool.class);
		ResponseListBean<EducationSchoolBean> responselistBean = new ResponseListBean<EducationSchoolBean>();
		for(EducationSchool educationSchool : list) {
			if(educationSchool!= null) {
				EducationSchoolBean educationSchoolBean = new EducationSchoolBean();
				BeanUtils.copyProperties(educationSchool, educationSchoolBean);
				responselistBean.getList().add(educationSchoolBean);
			}
		}
		return responselistBean;
	}

	/**
	 * @author Warren
	 * @param educationSchool
	 * @return ResponseListBean<EducationSchoolBean>
	 * @description 根据省市区选择所有学校
	 * @date Nov 8, 2018
	 */
	@Override
	public ResponseListBean<EducationSchoolBean> getListByProvinceAndCityAndArea(EducationSchool educationSchool) {
		String province = educationSchool.getProvince();
		String city = educationSchool.getCity();
		String area = educationSchool.getArea();
		String name = educationSchool.getName();
		List<EducationSchool> list = iEducationSchoolDao.findSchoolByProvinceAndCityAndArea(province, city, area,name);
		ResponseListBean<EducationSchoolBean> responselistBean = new ResponseListBean<EducationSchoolBean>();
		
		for(EducationSchool es : list) {
			if(es!= null) {
				EducationSchoolBean educationSchoolBean = new EducationSchoolBean();
				BeanUtils.copyProperties(es, educationSchoolBean);
				responselistBean.getList().add(educationSchoolBean);
			}
		}
		return responselistBean;
	}
	
	/**
	 * @author Warren
	 * @param id 学生id
	 * @param schoolId 学校id
	 * @return Map<String,Object>
	 * @description
	 * @date Nov 8, 2018
	 */
	@Transactional
	@Override
	public ResponseBean selectSchool(Long id,Long schoolId) {
		ResponseBean responseBean = new ResponseBean();
		boolean result = false;
		//获得学校
		EducationSchool educationSchool = iEducationSchoolDao.get(EducationSchool.class, schoolId);
		EducationStudent educationStudent = iEducationStudentDao.get(EducationStudent.class, id);
		educationStudent.setSchool(educationSchool);
		result = iEducationStudentDao.update(educationStudent);

		if(result) {
			responseBean.setStatus(ResponseStatus.SUCCESS.toString());
		}else {
			responseBean.setStatus(ResponseStatus.FAILED.toString());
		}
		return responseBean;
	}

	@Override
	@Transactional
	public Boolean addSchool(EducationSchool school) {
		
		return iEducationSchoolDao.save(school);
	}

	@Override
	public EducationSchoolResponse findSchoolById(Long studentId) {
		
		EducationSchool school = iEducationSchoolDao.get(EducationSchool.class, studentId);
		EducationSchoolResponse response = new EducationSchoolResponse();
		if (school!=null) {
			BeanUtils.copyProperties(school, response);
		}
		return response;
	}

	@Override
	@Transactional
	public Boolean updateSchool(EducationSchool educationSchool) {
		
		return iEducationSchoolDao.merge(educationSchool);
	}

	@Override
	@Transactional
	public Boolean deleteSchool(Long schoolId) {
		
		return iEducationSchoolDao.delete(schoolId, EducationSchool.class);
	}
	
}
